package com.sebiotec.book.ui.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.ViewModelProviders;

import com.sebiotec.basecommon.utils.base.BaseLazyLoadingFragment;
import com.sebiotec.basecommon.utils.entity.event.LoginEvent;
import com.sebiotec.basecommon.utils.tools.BookNameUtils;
import com.sebiotec.basecommon.utils.tools.ToastTool;
import com.sebiotec.book.R;
import com.sebiotec.book.R2;
import com.sebiotec.book.model.event.BasicEvent;
import com.sebiotec.book.model.response.UseProfileResponse;
import com.sebiotec.book.ui.activity.BasicActivity;
import com.sebiotec.book.ui.activity.LanguageActivity;
import com.sebiotec.book.ui.activity.UpdatePwdActivity;
import com.sebiotec.book.viewmodel.ProfileViewModel;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class ProfileFragment extends BaseLazyLoadingFragment {


    @BindView(R2.id.tv_profile_name)
    TextView tvProfileName;
    @BindView(R2.id.textView12)
    TextView textView12;
    @BindView(R2.id.iv_photo)
    ImageView ivPhoto;
    @BindView(R2.id.cl_base)
    ConstraintLayout clBase;
    @BindView(R2.id.cl_update_pwd)
    ConstraintLayout clUpdatePwd;
    @BindView(R2.id.cl_lang)
    ConstraintLayout clLang;
    @BindView(R2.id.cl_login_out)
    ConstraintLayout clLoginOut;
    private ProfileViewModel profileViewModel;
    private MiniLoadingDialog dialog;

    @Override
    protected String getLogTag() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_profile;
    }

    @Override
    protected void initView(View root) {
        profileViewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onFragmentResume() {
        super.onFragmentResume();
        if (dialog!=null){
            dialog.show();
        }else{
            dialog=new MiniLoadingDialog(getActivity(),getString(R.string.tip_loading_message));
            dialog.show();
        }
        initDatas();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dialog!=null){
            dialog.dismiss();
            dialog=null;
        }
        EventBus.getDefault().unregister(this);
    }


    public void initDatas() {
        profileViewModel.getUser(new ProfileViewModel.MyBasicDataCallBack<Object>() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                if (dialog!=null){
                    dialog.dismiss();
                }
                UseProfileResponse bean=(UseProfileResponse)o;
                tvProfileName.setText(bean.getEName());
                textView12.setText(BookNameUtils.getName(bean.getRoleId()));
            }

            @Override
            public void onDataFailCallBack(Object o) {
                if (dialog!=null){
                    dialog.dismiss();
                }
            }
        });
    }


    @OnClick({R2.id.iv_photo, R2.id.cl_base, R2.id.cl_update_pwd, R2.id.cl_lang,R2.id.cl_login_out})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_photo) {
        } else if (id == R.id.cl_base) {
            startActivity(BasicActivity.class);
        } else if (id == R.id.cl_update_pwd) {
            startActivity(UpdatePwdActivity.class);
        } else if (id == R.id.cl_lang) {
            startActivity(LanguageActivity.class);
        }else if (id==R.id.cl_login_out){
            new MaterialDialog.Builder(getActivity())
                    .content(getString(R.string.islogin_out))
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getResources().getColor(R.color.smj))
                    .positiveColor(getResources().getColor(R.color.smj))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText(getString(R.string.yes))
                    .negativeText(getString(R.string.no_login))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());
                            profileViewModel.LoginOut(new ProfileViewModel.MyBasicDataCallBack<Object>() {
                                @Override
                                public void onDataSuccessCallBack(Object o) {
                                    ToastTool.showSuccess(getContext(),getString(R.string.Login_Out));
                                    //发消息让Man关闭
                                    EventBus.getDefault().post(new LoginEvent(true));
                                }

                                @Override
                                public void onDataFailCallBack(Object o) {
                                    ToastTool.showError(getContext(),getString(R.string.Login_Out_Fail));
                                    //发消息让Man关闭
                                    EventBus.getDefault().post(new LoginEvent(true));
                                }
                            });

                        }
                    }).show();

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(BasicEvent event){
        tvProfileName.setText(event.getName());
    }


}
